// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Take6Model = require("Take6Model");

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },

        playerProgress: {
            default: null,
            type: cc.Sprite
        },

        m_nPlayerScore : 100,
        m_strPlayerName : "",
        m_nIconIndex : 0,
        m_flAddProgressPerSecond : 0.2,
        m_bRunProgress : false,
        m_nCountDownSum : 5,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad : function () 
    {

    },

    setPlayerScore : function (nScore)
    {
        this.m_nPlayerScore = nScore;

        var playerScoreLabel = this.node.getChildByName('playerScore');
        playerScoreLabel.getComponent(cc.Label).string = nScore.toString();

        var progressBarNode = this.node.getChildByName('progressBar');
        var progressBarSprite = progressBarNode.getComponent(cc.Sprite);
        progressBarSprite.fillRange = this.m_nPlayerScore > 0 ? this.m_nPlayerScore / 100 : 0;
    },

    setPlayerName : function (strName)
    {
        this.m_strPlayerName = strName;

        var playerName = this.node.getChildByName('playerName');
        playerName.getComponent(cc.Label).string = strName;
    },

    setPlayerIcon : function (nIconIndex)
    {
        this.m_nIconIndex = nIconIndex;

        var boardBaseNode = this.node.getChildByName('boardBase');
        var iconImageNode = boardBaseNode.getChildByName('iconImage');
        iconImageNode.setScale(0.87, 0.84);
        var imageScripte = iconImageNode.getComponent('IconImageScripte');
        imageScripte.setIcon(nIconIndex % 15);
    },

    setOffline : function (bOffline)
    {
        var offlineNode = this.node.getChildByName('offlineSprite');
        offlineNode.active = bOffline;
    },

    // start () {

    // },

    startProgress : function (progressEndFunction, nPlayerIndex)
    {
        this.m_progressEndFunction = progressEndFunction;
        this.m_nPlayerIndex = nPlayerIndex;
    	this.m_bRunProgress = true;
        this.playerProgress.fillRange = 0;

        var statusLabel = this.node.getChildByName('statusLabel');
        statusLabel.active = false;

        var countDownLabel = this.node.getChildByName('countDownLabel');
        countDownLabel.active = false;

    	this.progressCallBack = function () {
    		if (this.m_bRunProgress)
    		{
    			this.addProgress(this.playerProgress);
    		}
    		else
    		{
    			this.unschedule(this.progressCallBack);
    		}
    	};

     	this.schedule(this.progressCallBack, this.m_flAddProgressPerSecond);
    },

    clearProgress : function ()
    {
        this.m_bRunProgress = false;
        this.playerProgress.fillRange = 0;

        var statusLabel = this.node.getChildByName('statusLabel');
        statusLabel.active = false;

        var countDownLabel = this.node.getChildByName('countDownLabel');
        countDownLabel.active = false;
    },

    endProgress : function ()
    {
    	this.m_bRunProgress = false;
        this.playerProgress.fillRange = 1;

        var statusLabel = this.node.getChildByName('statusLabel');
        statusLabel.getComponent(cc.Label).string = "ready";
        statusLabel.active = true;

        var countDownLabel = this.node.getChildByName('countDownLabel');
        countDownLabel.active = false;

        this.unschedule(this.countDownCallBack);
    },

    addProgress : function (sprite)
    {
    	var nTimeSum = Take6Model.m_nWaitSendCardTime;

    	var fillRange = sprite.fillRange;
    	if (fillRange < 1)
    	{
    		fillRange += this.m_flAddProgressPerSecond / nTimeSum;
    	}

    	if (fillRange >= 1)
    	{
    		fillRange = 1;
    		sprite.fillRange = fillRange;
    		this.m_bRunProgress = false;

            this.m_progressEndFunction(this.m_nPlayerIndex);
    		this.startOfflineProgress();
    	}
    	else
    	{
    		sprite.fillRange = fillRange;
    	}
    },

    startOfflineProgress : function ()
    {
    	var countDownLabel = this.node.getChildByName('countDownLabel');
        countDownLabel.getComponent(cc.Label).string = this.m_nCountDownSum.toString();
        countDownLabel.active = true;

        this.countDownCallBack = function () {
    		if (this.m_nCountDownSum > 0)
    		{
    			this.m_nCountDownSum--;
    			var countDownLabel = this.node.getChildByName('countDownLabel');
        		countDownLabel.getComponent(cc.Label).string = this.m_nCountDownSum.toString();
    		}
    		else
    		{
    			var countDownLabel = this.node.getChildByName('countDownLabel');
        		countDownLabel.getComponent(cc.Label).string = "离线";
    			this.unschedule(this.countDownCallBack);
    		}
    	};

     	this.schedule(this.countDownCallBack, 1);
    },

    // update: function (dt) 
    // {
    //     if (this.playerProgress)
    //     {
    //         this._updateFillRange(this.playerProgress, 1, dt);
    //     }
    // },

    // _updateFillRange: function (sprite, range, dt) {
    //     var fillRange = sprite.fillRange;
    //     fillRange = fillRange < range ? fillRange += (dt * 0.1) : 0;
    //     sprite.fillRange = fillRange;
    // },

    // update (dt) {},
});
